from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import os.path
import glob
import time
import os

print("开始处理：")
start = time.time()

input_path = './else_test'
folderlist = os.listdir(input_path)          #列举文件夹
output_path = './else_test_resize_64'
if not os.path.exists(output_path):
	os.makedirs(output_path)

for fol in os.listdir(input_path):
	os.mkdir(output_path+'/'+fol)

for folder in folderlist:     
	inner_path = os.path.join(input_path, folder)
	outer_path = os.path.join(output_path, folder)
	total_num_folder = len(folderlist)       #文件夹的总数
	print('total have %d folders' % (total_num_folder))  #打印文件夹的总数
	
	filelist = os.listdir(inner_path)    #列举图片
	i = 0
	for item in filelist:
		total_num_file = len(filelist)    #单个文件夹内图片的总数
		if item.endswith('.jpg'):
			src = os.path.join(os.path.abspath(inner_path), item)    #原图的地址
			dst = os.path.join(os.path.abspath(outer_path), item)
			print(dst)
			try:
				img=Image.open(src)
				new_img=img.resize((64,64),Image.BICUBIC)  #resize的大小参数
				new_img.save(dst, quality=95, subsampling=0)  #保存照片的质量，默认为无损
				# print('converting %s to %s ...' % (src, dst))
				i += 1
			except:
				continue
	print('total %d to resize & converted %d jpgs' % (total_num_file, i))

end = time.time()
print("处理完成，resize照片耗时: %s分" % ((end - start) / 60))